﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMark.DAO
{
    public class TinTucModels
    {
        public static List<BAI_VIET> LayTatCaBaiViet()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<BAI_VIET> BaiViet = context.BAI_VIET.Include("NGUOI_DUNG").OrderByDescending(o => o.NgayTao).ToList();
                foreach (var item in BaiViet)
                {
                    if (item.Hinh == null)
                        item.Hinh = "image2.jpg";
                }
                return BaiViet;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
        public static BAI_VIET LayBaiVietTheoID(long ID)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                BAI_VIET BaiViet = context.BAI_VIET.Include("NGUOI_DUNG").Where(w=>w.ID==ID).First();
                if (BaiViet.Hinh != null)
                    BaiViet.Hinh = "image2.jpg";
                return BaiViet;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
        public static List<BAI_VIET> LayBaiVietTheoTacGia(long IDTacGia)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<BAI_VIET> BaiViet = context.BAI_VIET.Include("NGUOI_DUNG").Where(w=>w.NguoiTao == IDTacGia).OrderByDescending(o => o.NgayTao).ToList();
                foreach (var item in BaiViet)
                {
                    if (item.Hinh == null)
                        item.Hinh = "image2.jpg";
                }
                return BaiViet;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
        public static List<LOAI_BAI_VIET> LayTatCaDanhMucBaiViet()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<LOAI_BAI_VIET> LoaiBaiViet = context.LOAI_BAI_VIET.OrderBy(o => o.ThuTu).ToList();

                return LoaiBaiViet;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
        public static List<BAI_VIET> LayBaiVietTheoLoai(long IDLoaiBV)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<BAI_VIET> BaiViet = context.BAI_VIET.Include("NGUOI_DUNG").Where(w=>w.IDLoaiBaiViet==IDLoaiBV).OrderByDescending(o => o.NgayTao).ToList();
                foreach (var item in BaiViet)
                {
                    if (item.Hinh == null)
                        item.Hinh = "image2.jpg";
                }
                return BaiViet;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
    }
}